package com.ygqh.baby.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgLogisticsMapper;
import com.ygqh.baby.po.YgLogistics;
import com.ygqh.baby.po.YgLogisticsExample;
import com.ygqh.baby.po.YgLogisticsExample.Criteria;
import com.ygqh.baby.service.YgLogisticsService;

@Service
public class YgLogisticsServiceImpl implements YgLogisticsService {

	@Autowired
	private YgLogisticsMapper ygLogisticsMapper;

	@Override
	public List<YgLogistics> find() {
		YgLogisticsExample example = new YgLogisticsExample();
		example.createCriteria();
		return ygLogisticsMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgLogisticsExample example = new YgLogisticsExample();
		example.createCriteria();
		return ygLogisticsMapper.countByExample(example);
	}

	@Override
	public YgLogistics findById(Long id) {
		return ygLogisticsMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<YgLogistics> findByOrderId(String orderId) {
		YgLogisticsExample example = new YgLogisticsExample();
		example.createCriteria().andOrderIdEqualTo(orderId);
		example.setOrderByClause("create_time desc");
		return ygLogisticsMapper.selectByExample(example);
	}

	@Override
	public int save(YgLogistics ygLogistics) {
		return ygLogisticsMapper.insertSelective(ygLogistics);
	}

	@Override
	public int update(YgLogistics ygLogistics) {
		return ygLogisticsMapper.updateByPrimaryKeySelective(ygLogistics);
	}

	@Override
	public ResultSet<YgLogistics> search(QueryInfo queryInfo, String q) {
		List<YgLogistics> list = ygLogisticsMapper.selectSuper(queryInfo, q);
		int count = ygLogisticsMapper.countSuper(q);
		ResultSet<YgLogistics> resultSet = new ResultSet<YgLogistics>(count, list);
		return resultSet;
	}

	@Override
	public int saveOrUpdate(YgLogistics ygLogistics) {
		int r = 0;
		if (ygLogistics.getId() != null) {
			r = this.update(ygLogistics);
		} else {
			r = this.save(ygLogistics);
		}
		return r;
	}

	@Override
	public int addLogisticsBatch(List<YgLogistics> logisticsList) {
		if (logisticsList.size() > 0) {
			int batch = ygLogisticsMapper.addLogisticsBatch(logisticsList);
			return batch;
		}
		return 0;
	}

	private List<YgLogistics> removeDuplicteLogistics(List<YgLogistics> logisticsList) {
		if (CollectionUtils.isEmpty(logisticsList)) {
			return logisticsList;
		}
		TreeSet<YgLogistics> set = new TreeSet<YgLogistics>(new Comparator<YgLogistics>() {

			@Override
			public int compare(YgLogistics o1, YgLogistics o2) {
				String str1 = o1.getOrderId() + o1.getExpressCode();
				String str2 = o2.getOrderId() + o2.getExpressCode();
				if (str1.equals(str2)) {
					return 0;
				}
				return 1;
			}
		});
		set.addAll(logisticsList);
		return new ArrayList<YgLogistics>(set);

	}

	@Override
	public List<YgLogistics> findLogistics(Long userId, Date startDate, Date endDate) {
		return ygLogisticsMapper.selectLogistics(userId, startDate, endDate);
	}

	@Override
	public List<YgLogistics> findByOrderId(Long orderId, Long supplierId) {

		return ygLogisticsMapper.selectLogisticsBySupplierId(orderId, supplierId);
	}

	@Override
	public List<YgLogistics> findByExpressCode(String expressCode) {

		return this.findByExpressCode(null, expressCode);
	}

	@Override
	public List<YgLogistics> findByExpressCode(String orderCode, String expressCode) {
		YgLogisticsExample example = new YgLogisticsExample();
		Criteria criteria = example.createCriteria().andExpressCodeEqualTo(expressCode);
		if (StringUtil.isNotBlank(orderCode)) {
			criteria.andOrderIdEqualTo(orderCode);
		}
		return ygLogisticsMapper.selectByExample(example);
	}

	@Override
	public Boolean isExist(String orderCode, String expressCode) {
		YgLogisticsExample example = new YgLogisticsExample();
		Criteria criteria = example.createCriteria().andExpressCodeEqualTo(expressCode);
		if (StringUtil.isNotBlank(orderCode)) {
			criteria.andOrderIdEqualTo(orderCode);
		}
		int i = ygLogisticsMapper.countByExample(example);

		return i > 0 ? true : false;
	}

}
